package com.feonix.common.view.component;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.feonix.common.BaseInfo;

/**
 * 设置全局的js，css放到对象中
 * 
 * @author Administrator
 *
 */
public class ComponentConfiger {

	private static Map<String, ComponentConfig> componentConfigMap = new HashMap<String, ComponentConfig>();

	public static void initComponent() {
		String componentPath = ComponentConfiger.class.getClassLoader().getResource("").getPath()
				+ "pub/config/component.xml";
		load(componentPath);
	}

	public static void load(String paramString) {
		File file = new File(paramString);
		try {
			Document document = Jsoup.parse(file, "UTF-8");
			Elements elements1 = document.getElementsByTag("component");
			String contextPath = BaseInfo.getContextPath();
			if ((elements1 == null) || (elements1.size() <= 0)) {
				return;
			}

			Iterator<Element> iterator1 = elements1.iterator();
			while (iterator1.hasNext()) {
				Element element1 = (Element) iterator1.next();
				String componentId = element1.attr("id");
				ArrayList<HashMap<String, String>> cssList = new ArrayList<HashMap<String, String>>();
				ArrayList<HashMap<String, String>> jsList = new ArrayList<HashMap<String, String>>();
				Elements element2 = element1.children();
				String[] arrayOfString = null;
				Iterator<Element> iterator2 = element2.iterator();
				while (iterator2.hasNext()) {
					Element element3 = (Element) iterator2.next();
					String str5 = element3.text();
					if (!(str5.startsWith("http://"))) {
						str5 = contextPath + str5;
					}
					String tagName = element3.tagName();
					if (tagName.equals("css")) {
						HashMap<String, String> obj = new HashMap<String, String>();
						obj.put("path", str5);
						obj.put("browser", element3.attr("browser"));
						cssList.add(obj);
					} else if (tagName.equals("js")) {
						HashMap<String, String> obj = new HashMap<String, String>();
						obj.put("path", str5);
						obj.put("browser", element3.attr("browser"));
						jsList.add(obj);
					} else {
						String text = element3.text();
						if ((text != null) && (text.length() > 0))
							arrayOfString = text.split(",");
					}
					ComponentConfig obj = new ComponentConfig();
					obj.setCss(cssList);
					obj.setJs(jsList);
					obj.setDependencies(arrayOfString);
					componentConfigMap.put(componentId, obj);
				}
			}
		} catch (Exception exception) {
			LogFactory.getLog(ComponentConfiger.class).error(exception.getMessage(), exception);
		}
	}

	public static StringBuffer buildCode(String paramString, Set<String> paramSet) {
		ComponentConfig componentConfig = componentConfigMap.get(paramString);
		ArrayList<HashMap<String, String>> cssList = componentConfig.getCss();
		ArrayList<HashMap<String, String>> jsList = componentConfig.getJs();
		String[] arrayOfString = componentConfig.getDependencies();
		StringBuffer stringBuffer = new StringBuffer();
		int i = 0;

		if ((arrayOfString != null) && (arrayOfString.length > 0)) {
			for (i = 0; i < arrayOfString.length; ++i) {
				String str = arrayOfString[i];
				if (!(paramSet.contains(str))) {
					stringBuffer.append(buildCode(str, paramSet));
				}
			}
		}

		if ((cssList != null) && (cssList.size() > 0)) {
			for (i = 0; i < cssList.size(); ++i) {
				stringBuffer.append(buildCss(cssList.get(i)));
			}
		}

		if ((jsList != null) && (jsList.size() > 0)) {
			for (i = 0; i < jsList.size(); ++i) {
				stringBuffer.append(buildJs(jsList.get(i)));
			}
		}
		paramSet.add(paramString);
		return stringBuffer;
	}

	private static String buildCss(HashMap<String, String> paramHashMap) {
		String str1 = paramHashMap.get("path");
		String str2 = paramHashMap.get("browser");
		StringBuilder stringBuffer = new StringBuilder();

		if ((str2 == null) || (str2.trim().length() == 0)) {
			stringBuffer.append("<link type=\"text/css\" rel=\"stylesheet\" href=\"" + str1 + "\"/>\n");
		} else {
			stringBuffer.append("<!--[if " + str2 + "]>\n");
			stringBuffer.append("<link type=\"text/css\" rel=\"stylesheet\" href=\"" + str1 + "\"/>\n");
			stringBuffer.append("<![endif]-->\n");
		}
		return stringBuffer.toString();
	}

	private static String buildJs(HashMap<String, String> paramHashMap) {
		String str1 = paramHashMap.get("path");
		String str2 = paramHashMap.get("browser");
		StringBuilder stringBuffer = new StringBuilder();

		if ((str2 == null) || (str2.trim().length() == 0)) {
			stringBuffer.append("<script type=\"text/javascript\" src=\"" + str1 + "\"></script>\n");
		} else {
			stringBuffer.append("<!--[if " + str2 + "]>\n");
			stringBuffer.append("<script type=\"text/javascript\" src=\"" + str1 + "\"></script>\n");
			stringBuffer.append("<![endif]-->\n");
		}
		return stringBuffer.toString();
	}

}
